package com.elai.tags.service.impl;

import java.util.List;
import com.elai.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.elai.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.elai.tags.domain.BaseUserPortraitCompents;
import com.elai.tags.mapper.BaseUserPortraitMapper;
import com.elai.tags.domain.BaseUserPortrait;
import com.elai.tags.service.IBaseUserPortraitService;

/**
 * 用户分群Service业务层处理
 * 
 * @author elai
 * @date 2021-10-09
 */
@Service
public class BaseUserPortraitServiceImpl implements IBaseUserPortraitService 
{
    @Autowired
    private BaseUserPortraitMapper baseUserPortraitMapper;

    /**
     * 查询用户分群
     * 
     * @param id 用户分群ID
     * @return 用户分群
     */
    @Override
    public BaseUserPortrait selectBaseUserPortraitById(Integer id)
    {
        return baseUserPortraitMapper.selectBaseUserPortraitById(id);
    }

    /**
     * 查询用户分群列表
     * 
     * @param baseUserPortrait 用户分群
     * @return 用户分群
     */
    @Override
    public List<BaseUserPortrait> selectBaseUserPortraitList(BaseUserPortrait baseUserPortrait)
    {
        return baseUserPortraitMapper.selectBaseUserPortraitList(baseUserPortrait);
    }

    /**
     * 新增用户分群
     * 
     * @param baseUserPortrait 用户分群
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBaseUserPortrait(BaseUserPortrait baseUserPortrait)
    {
        baseUserPortrait.setCreateTime(DateUtils.getNowDate());
        int rows = baseUserPortraitMapper.insertBaseUserPortrait(baseUserPortrait);
        insertBaseUserPortraitCompents(baseUserPortrait);
        return rows;
    }

    /**
     * 修改用户分群
     * 
     * @param baseUserPortrait 用户分群
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBaseUserPortrait(BaseUserPortrait baseUserPortrait)
    {
        baseUserPortrait.setUpdateTime(DateUtils.getNowDate());
        baseUserPortraitMapper.deleteBaseUserPortraitCompentsByPortraitId(baseUserPortrait.getId());
        insertBaseUserPortraitCompents(baseUserPortrait);
        return baseUserPortraitMapper.updateBaseUserPortrait(baseUserPortrait);
    }

    /**
     * 批量删除用户分群
     * 
     * @param ids 需要删除的用户分群ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBaseUserPortraitByIds(Integer[] ids)
    {
        baseUserPortraitMapper.deleteBaseUserPortraitCompentsByPortraitIds(ids);
        return baseUserPortraitMapper.deleteBaseUserPortraitByIds(ids);
    }

    /**
     * 删除用户分群信息
     * 
     * @param id 用户分群ID
     * @return 结果
     */
    @Override
    public int deleteBaseUserPortraitById(Integer id)
    {
        baseUserPortraitMapper.deleteBaseUserPortraitCompentsByPortraitId(id);
        return baseUserPortraitMapper.deleteBaseUserPortraitById(id);
    }

    /**
     * 新增画像组件信息
     * 
     * @param baseUserPortrait 用户分群对象
     */
    public void insertBaseUserPortraitCompents(BaseUserPortrait baseUserPortrait)
    {
        List<BaseUserPortraitCompents> baseUserPortraitCompentsList = baseUserPortrait.getPortraitTemplateItems();
        Long id = Long.valueOf(baseUserPortrait.getId());
        if (StringUtils.isNotNull(baseUserPortraitCompentsList))
        {
            List<BaseUserPortraitCompents> list = new ArrayList<BaseUserPortraitCompents>();
            for (BaseUserPortraitCompents baseUserPortraitCompents : baseUserPortraitCompentsList)
            {
                baseUserPortraitCompents.setPortraitId(id);
                list.add(baseUserPortraitCompents);
            }
            if (list.size() > 0)
            {
                baseUserPortraitMapper.batchBaseUserPortraitCompents(list);
            }
        }
    }
}
